'use strict';

// const util = require('../utils/util');
const Controller = require('egg').Controller;
let temp_obj = {};
class HomeController extends Controller {
  // 获取短信验证码
  /**
     * @api {post} /api/sms 获取短信验证码
     * @apiGroup 验证码
     * @apiParam {String} mobile 手机号码
     * @apiParamExample 参数模板
     {
       mobile: 13111111111
     }
     * @apiSuccessExample  成功返回
    {
        "msg": "成功操作",
        "result": {
          code: 1234
        }
    }
     * @apiErrorExample  错误返回
     * {
     *   "msg": ""
     * }
     */

  async sms() {
    const { ctx } = this;
    // const dataObj = ctx.request.body;
    if (new ctx.helper.checkData(this, 'mobile').checkDataRes) {
      ctx.body = ctx.response.success({ code: 1234 });
    }
  }
  // 获取图片验证码
  /**
     * @api {Get} /api/verify 获取图片验证码
     * @apiGroup 验证码
     * @apiParamExample 参数模板
     {}
     * @apiSuccessExample  成功返回
    {
        svg图片
    }
     * @apiErrorExample  错误返回
     {
        "text": "lEYC",
          "data": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"100\" height=\"40\" viewBox=\"0,0,100,40\"><path d=\"M3 11 C68 37,43 13,97 36\" stroke=\"#222\" fill=\"none\"/><path fill=\"#222\" d=\"M15.01 10.21L15.12 10.32L15.12 10.32Q15.90 12.05 17.40 12.36L17.41 12.37L17.34 12.30Q17.86 12.45 18.48 12.28L18.48 12.28L18.41 12.21Q18.37 17.65 18.30 22.88L18.26 22.84L18.28 22.86Q18.14 27.92 18.34 33.22L18.28 33.16L18.35 33.23Q18.16 33.25 17.89 33.25L17.78 33.14L17.84 33.19Q16.39 33.14 15.24 34.78L15.38 34.92L15.24 34.78Q16.01 28.74 15.97 22.63L15.85 22.51L15.92 22.57Q15.78 16.35 15.04 10.24ZM14.99 35.92L14.99 35.92L14.95 35.88Q15.91 34.26 16.86 33.85L16.77 33.76L16.78 33.76Q16.79 34.76 16.79 35.65L16.73 35.59L16.71 35.56Q17.34 35.21 18.06 35.32L18.09 35.35L18.16 35.42Q19.88 35.61 20.97 38.02L21.07 38.13L21.07 38.12Q19.95 31.33 19.95 24.67L20.07 24.79L19.91 24.62Q20.07 17.88 20.81 11.36L20.77 11.32L20.69 11.24Q19.83 12.96 18.81 13.50L18.83 13.52L18.90 13.59Q18.77 12.54 18.84 11.59L18.96 11.71L18.92 11.67Q18.19 12.10 17.51 11.97L17.53 11.99L17.51 11.96Q15.48 11.53 14.59 8.98L14.55 8.94L14.70 9.08Q15.63 15.80 15.70 22.53L15.56 22.38L15.63 22.45Q15.69 29.11 14.91 35.84Z\"/><path fill=\"#222\" d=\"M74.85 33.32L74.91 33.39L74.94 33.42Q71.08 32.78 69.45 31.49L69.45 31.50L69.45 31.50Q67.89 30.27 67.51 27.59L67.39 27.47L67.54 27.62Q67.35 27.12 67.04 22.87L67.06 22.89L67.22 23.05Q67.10 21.84 67.03 20.72L67.12 20.80L67.20 20.88Q67.01 16.34 68.98 14.78L68.95 14.75L68.87 14.66Q71.39 12.94 78.32 12.32L78.35 12.35L78.34 12.34Q79.51 12.18 80.97 12.22L80.95 12.20L80.91 12.16Q80.89 12.14 83.54 12.14L83.61 12.21L83.56 12.16Q84.20 12.05 85.73 12.19L85.87 12.33L85.91 12.37Q85.46 13.21 84.54 15.96L84.40 15.82L84.48 15.90Q82.56 14.77 79.88 14.77L80.01 14.89L80.01 14.90Q79.15 14.71 78.43 14.78L78.42 14.77L78.59 14.94Q74.05 15.36 71.80 17.12L71.81 17.13L71.71 17.03Q69.89 18.44 69.75 21.43L69.86 21.54L69.68 21.36Q69.77 22.03 69.81 23.15L69.73 23.08L69.74 23.08Q69.86 27.42 72.00 29.29L71.93 29.21L72.02 29.31Q74.01 31.09 78.46 31.36L78.38 31.28L78.48 31.38Q81.28 31.53 83.96 29.93L83.94 29.91L83.90 29.87Q84.37 32.24 84.91 33.46L84.99 33.54L85.03 33.58Q83.11 33.77 81.65 33.74L81.61 33.70L81.61 33.69Q77.70 33.73 74.92 33.39ZM88.39 36.71L88.33 36.64L88.39 36.70Q86.81 34.03 86.19 31.72L86.11 31.63L86.20 31.73Q85.72 31.86 84.84 32.20L84.95 32.31L84.87 32.23Q84.58 31.53 84.41 30.78L84.31 30.69L84.01 29.20L84.11 29.30Q81.22 31.17 78.43 31.06L78.39 31.02L78.34 30.97Q74.56 30.83 72.49 29.26L72.44 29.22L72.43 29.21Q71.49 27.66 71.56 25.04L71.60 25.08L71.48 24.96Q71.68 21.02 73.89 19.11L73.91 19.13L73.77 18.99Q75.71 17.33 79.90 16.79L79.93 16.82L79.85 16.74Q80.65 16.72 81.29 16.72L81.30 16.73L81.34 16.77Q83.87 16.82 85.74 18.18L85.60 18.04L85.72 18.16Q86.14 16.24 87.23 13.35L87.26 13.37L87.14 13.25Q86.96 13.32 86.47 13.27L86.52 13.31L86.52 13.32Q86.03 13.27 85.79 13.27L85.67 13.15L85.73 13.21Q85.95 12.79 86.36 11.90L86.35 11.89L86.33 11.86Q86.04 11.78 83.81 11.72L83.89 11.80L83.78 11.69Q81.69 11.75 81.11 11.78L81.04 11.71L81.05 11.72Q71.67 12.03 68.58 14.34L68.66 14.42L68.66 14.42Q66.75 15.94 66.75 19.45L66.80 19.50L66.77 19.46Q66.81 20.49 66.91 22.91L66.81 22.81L66.87 22.86Q66.94 25.99 67.14 27.52L67.21 27.59L67.28 27.66Q67.63 30.46 69.06 31.82L68.95 31.70L68.87 31.63Q69.50 32.43 70.62 33.45L70.50 33.33L70.62 33.45Q73.44 34.94 78.13 35.62L78.20 35.69L78.18 35.67Q83.88 36.48 88.33 36.65Z\"/><path fill=\"#222\" d=\"M58.96 33.20L58.94 33.18L57.38 33.34L57.39 33.36Q56.47 33.29 55.62 33.39L55.65 33.42L55.60 33.37Q56.20 27.92 56.20 24.35L56.01 24.16L56.12 24.27Q52.60 18.34 45.77 10.59L45.73 10.55L45.68 10.50Q47.36 11.47 50.35 12.18L50.35 12.18L50.39 12.22Q54.79 17.09 57.47 21.75L57.62 21.89L57.52 21.80Q60.20 16.83 64.11 12.34L64.02 12.25L64.07 12.30Q66.35 12.06 68.59 11.35L68.59 11.35L68.52 11.28Q63.17 16.77 58.82 24.28L58.66 24.13L58.67 24.13Q58.77 30.52 59.04 33.28ZM67.17 13.13L69.55 10.54L69.46 10.46Q66.71 11.51 63.92 11.91L64.10 12.10L63.97 11.96Q60.20 16.18 57.69 20.74L57.65 20.70L57.80 20.85Q55.73 17.43 52.88 13.96L52.92 14.00L52.95 14.04Q52.80 14.12 52.36 14.05L52.27 13.96L52.29 13.98Q51.58 13.17 50.22 11.68L50.42 11.87L50.23 11.68Q47.32 11.12 44.66 9.72L44.62 9.68L44.58 9.63Q51.48 17.22 55.73 24.29L55.70 24.25L55.83 24.39Q55.75 30.06 55.21 33.76L55.22 33.77L55.06 33.61Q55.83 33.70 57.12 33.60L57.07 33.54L57.11 34.50L57.09 34.48Q57.20 35.01 57.14 35.48L57.07 35.42L56.95 35.30Q60.12 35.31 61.68 35.48L61.83 35.63L61.72 35.52Q60.63 31.36 60.52 26.02L60.63 26.13L60.46 25.96Q64.44 18.49 70.29 11.93L70.35 11.99L67.13 13.09Z\"/><path fill=\"#222\" d=\"M29.32 10.41L29.30 10.39L29.35 10.44Q32.79 12.76 37.04 12.97L37.00 12.92L37.00 12.93Q41.12 13.07 44.93 11.50L44.96 11.53L44.87 11.44Q44.71 11.99 44.03 14.48L44.09 14.54L44.06 14.51Q41.62 15.44 38.97 15.58L39.01 15.62L39.00 15.60Q36.26 15.65 33.67 15.00L33.79 15.11L33.75 15.07Q34.31 18.25 34.48 21.51L34.57 21.61L34.56 21.60Q36.34 21.78 37.77 21.75L37.74 21.72L37.81 21.79Q38.74 21.63 40.88 21.53L40.92 21.57L40.95 21.60Q40.99 22.25 40.99 22.86L40.96 22.83L40.96 24.02L40.82 23.89Q38.75 23.95 34.46 24.02L34.52 24.07L34.41 23.96Q34.44 27.66 33.93 31.23L33.97 31.27L33.90 31.20Q36.27 30.65 38.72 30.75L38.72 30.75L38.83 30.87Q41.27 30.96 43.58 31.70L43.42 31.55L43.47 31.59Q43.55 32.32 43.79 33.00L43.79 33.00L44.20 34.36L44.20 34.37Q41.35 33.22 38.29 33.22L38.36 33.28L38.37 33.30Q33.85 33.13 30.05 35.13L30.21 35.29L30.13 35.22Q32.28 29.21 32.04 22.85L32.00 22.81L32.01 22.82Q31.76 16.42 29.38 10.47ZM29.71 35.88L29.80 35.97L29.72 35.89Q30.81 35.22 31.49 34.91L31.58 35.00L31.46 34.88Q31.39 35.66 30.88 37.02L30.76 36.90L30.74 36.88Q34.87 35.20 39.39 35.44L39.39 35.43L39.32 35.36Q43.97 35.66 47.54 38.08L47.50 38.04L47.57 38.11Q46.57 35.89 45.96 34.16L45.99 34.19L45.94 34.13Q44.95 33.48 44.06 33.17L44.22 33.33L44.10 33.21Q43.81 32.03 43.67 31.32L43.75 31.40L43.60 31.25Q40.92 30.44 38.10 30.44L38.17 30.51L38.18 30.52Q37.13 30.49 36.11 30.59L36.01 30.49L36.05 30.53Q36.15 29.40 36.18 28.32L36.29 28.42L36.23 26.06L36.23 26.05Q37.80 25.92 39.39 25.95L39.56 26.12L39.52 26.09Q41.12 26.08 42.71 26.22L42.78 26.28L42.63 26.13Q42.63 25.35 42.63 24.50L42.61 24.49L42.71 22.88L41.90 22.85L41.89 22.84Q41.56 22.96 41.15 22.96L41.08 22.88L41.20 23.01Q41.15 22.34 41.28 21.18L41.20 21.10L41.27 21.17Q39.78 21.31 38.73 21.35L38.87 21.49L38.74 21.35Q37.96 21.43 36.33 21.36L36.36 21.39L36.39 21.42Q36.32 20.16 36.15 17.64L36.09 17.59L36.09 17.59Q36.92 17.60 37.80 17.60L37.97 17.77L37.89 17.69Q42.10 17.68 45.40 15.98L45.47 16.05L45.40 15.99Q45.99 13.96 46.77 11.99L46.78 12.00L46.70 11.92Q45.52 12.67 44.77 12.98L44.83 13.03L44.84 13.05Q44.99 12.25 45.40 10.86L45.52 10.98L45.40 10.85Q41.21 12.71 36.86 12.44L36.92 12.50L37.01 12.59Q32.12 12.15 28.68 9.60L28.87 9.79L28.69 9.61Q31.29 15.84 31.56 22.64L31.62 22.71L31.68 22.76Q31.91 29.79 29.66 35.84Z\"/></svg>"
      }
     */

  async verify() {
    const { ctx } = this;
    const captcha = await ctx.service.com.tools.captcha();
    ctx.response.type = 'image/svg+xml'; // 返回的类型
    ctx.body = captcha; // 返回一张图片
  }
  // 获取所有城市
  /**
     * @api {Get} /api/city 获取所有城市
     * @apiGroup 城市
     * @apiParamExample 参数模板
     {}
     * @apiSuccessExample  成功返回
    {
        "msg": "成功操作",
        "result": []
    }
     * @apiErrorExample  错误返回
     * {
     *   "msg": ""
     * }
     */

  async getCity() {
    const { ctx } = this;
    const { City } = this.app.model.Tables.Shop;
    const result = await City.findAll();
    if (result) {
      const data = JSON.parse(JSON.stringify(result));
      temp_obj = {};
      data.forEach((val) => {
        val.value = val.id;
        val.key = val.id;
        val.label = val.cityname;
        temp_obj[val.pid]
          ? temp_obj[val.pid].push(val)
          : (temp_obj[val.pid] = [val]);
      });
      ctx.body = ctx.response.success(this.melon());
    } else {
      ctx.body = ctx.response.error('获得城市失败');
    }
  }

  melon(pid = 1) {
    if (temp_obj.hasOwnProperty(pid)) {
      const temp_arr = temp_obj[pid];
      temp_arr.forEach((val, index) => {
        const second_arr = this.melon(val.id);
        second_arr.length && (temp_arr[index].children = second_arr);
      });
      return temp_arr;
    }
    return [];
  }
}

module.exports = HomeController;
